import { getStorage, setStorage } from './../utils/storage'
import { loginFn } from '@/api/user'
export default {
  namespaced: true,
  state: {
    loginState: getStorage('loginState') === 'true' || false,
    userid: getStorage('userid') || '',
    token: getStorage('token') || ''
  },
  mutations: {
    changeLoginState (state, data) {
      state.loginState = data
    },
    changeUserid (state, data) {
      state.userid = data
    },
    changeToken (state, data) {
      state.token = data
    }
  },
  actions: {
    async loginAction (context, payload) {
      const { loginname, password } = payload
      const res = await loginFn({ loginname, password })
      if (res.data.code === '10010') {
        return '10010'
      } else if (res.data.code === '10011') {
        return '10011'
      } else {
        context.commit('changeLoginState', true)
        context.commit('changeUserid', res.data.data.userid)
        context.commit('changeToken', res.data.data.token)
        setStorage('loginState', true)
        setStorage('userid', res.data.data.userid)
        setStorage('token', res.data.data.token)
        return ''
      }
    }
  }
}
